#include "facepic.h"
#include "ui_facepic.h"
#include "facedetector.h"
#include "facekeypointdetector.h"
#include <QDateTime>

using namespace std;
using namespace cv;
facepic::facepic(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::facepic)
{
    ui->setupUi(this);
    QString s_tmp = "/apps/VideoCaptureQt/2022-04-26_22-39-30_1.jpg";

    img.load(s_tmp);

    image_fliped = Mat(img.height(), img.width(), CV_8UC3,(void*)img.constBits(),img.bytesPerLine());


    ui->imglabel->setPixmap(QPixmap::fromImage(img));
    ui->imglabel->resize(img.size());
    ui->imglabel->show();
}

facepic::~facepic()
{
    delete ui;
}

int test_picture(QImage& image,Mat& image_fliped){
    int Param1,  Param2, Param3, Param4;

    std::string detector_param = "/apps/VideoCaptureQt/data/face.param";
    std::string detector_bin = "/apps/VideoCaptureQt/data/face.bin";

    static FaceDetector* faceDetector = new FaceDetector(detector_param, detector_bin);

    std::vector<Bbox> boxes;

    faceDetector->Detect(image, boxes);
    if (boxes.size() == 0)
    {
        return -1;
    }
    else if (boxes.size() > 1)
    {
        return -2;
    }
    Param1 = int(boxes[0].x1);
    Param2 = int(boxes[0].y1);
    Param3 = int(boxes[0].x2);
    Param4 = int(boxes[0].y2);

    cout<<"Param1 "<<Param1<<endl;
    cout<<"Param2 "<<Param2<<endl;
    cout<<"Param3 "<<Param3<<endl;
    cout<<"Param4 "<<Param4<<endl;


    QPainter painter(&image);
    QPen pen;
    pen.setWidth(6);
    pen.setColor(Qt::green); // 设置画笔为黄色
    painter.setPen(pen); // 设置画笔

    QRect rect= QRect(boxes[0].x1, boxes[0].y1, boxes[0].x2 - boxes[0].x1 + 1, boxes[0].y2 - boxes[0].y1 + 1);
    painter.drawRect(rect);

    cout<<"ok"<<endl;

    return 0;
}

void facepic::on_openimage_clicked()
{
    QString s_tmp = "/apps/VideoCaptureQt/2022-04-26_22-39-30_1.jpg";

    img.load(s_tmp);
    QDateTime start = QDateTime::currentDateTime();


    test_picture(img,image_fliped);
    QDateTime end = QDateTime::currentDateTime();

    qint64 intervalTimeMS = start.msecsTo(end);
    cout<<intervalTimeMS<<endl;
//    cvtColor(image_fliped, image_fliped, COLOR_BGR2RGB);
//    QImage image1 = QImage((uchar*)(image_fliped.data), image_fliped.cols, image_fliped.rows, QImage::Format_RGB888);

//    img = QImage((const unsigned char*)image_fliped.data, image_fliped.cols, image_fliped.rows, image_fliped.step, QImage::Format_RGB888);

//    ui->imglabel->setPixmap(QPixmap::fromImage(image1));
//    ui->imglabel->resize(image1.size());
//    ui->imglabel->show();
    ui->imglabel->setPixmap(QPixmap::fromImage(img));

}
